package com.right.health.app.ui.me;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.SocialNumberResponse;
import com.right.health.app.bean.SocialSecuritySign;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.net.UserApi;

import essclib.pingan.ai.request.biap.Biap;
import hc.mhis.paic.com.essclibrary.EsscSDK;
import hc.mhis.paic.com.essclibrary.listener.ESSCCallBack;

/*
 * 社保卡
 * Created by hopeliao on 2019-10-01
 *
 */
public class SocialSecurityInfoActivity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private static final int HTTP_CODE_GET = 1;
    private static final int HTTP_CODE_SIGN_CODE = 2;

    private UserApi mUserApi;

    private boolean mAllowAdd = false;

    private SocialNumberResponse.SocialNumberInfo mResponse;

    private TextView mUserNameTv, mSocialSecurityNumberTv;
    private TextView mCardIssueTv, mIdcardTv;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.social_security_info);
    }

    @Override
    public void findView() {
        findViewById(R.id.ib_add).setOnClickListener(this);

        mUserNameTv = (TextView) findViewById(R.id.tv_name);
        mSocialSecurityNumberTv = (TextView) findViewById(R.id.tv_social_security_number);
        mCardIssueTv = (TextView) findViewById(R.id.tv_card_issue);
        mIdcardTv = (TextView) findViewById(R.id.tv_idcard);

        findViewById(R.id.tv_bind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUserApi.addRequestCode(HTTP_CODE_SIGN_CODE);
                mUserApi.getSignCode();
            }
        });
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);

        refreshNet();
    }

    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if(intent == BroadCastCode.REFRESH_COMMON) {
            mAllowAdd = false;
            refreshNet();
        }
    }

    private void refreshNet() {
        mUserApi.addRequestCode(HTTP_CODE_GET);
        mUserApi.getSocialSecurity();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_add:
                if(mAllowAdd) {
                    startIntent(SocialSecurityCardActivity.class);
                }
                break;
        }
    }

    private void refreshUI() {
        if(mResponse != null) {
            mUserNameTv.setText(mResponse.realname);
            mSocialSecurityNumberTv.setText(mResponse.phone);
//            mCardIssueTv.setText(mResponse.idcard);
            mIdcardTv.setText(mResponse.idcard);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(response.isResponseFail()) {
            findViewById(R.id.empty_view).setVisibility(View.VISIBLE);
            findViewById(R.id.scroll_view).setVisibility(View.INVISIBLE);

            ToastUtil.show(this, response.error);
        } else {
            if(request.requestCode == HTTP_CODE_GET) {
                if(response.targetData instanceof SocialNumberResponse) {
                    SocialNumberResponse socialNumberResponse = response.cast(SocialNumberResponse.class);
                    if(CollectionUtil.isEmpty(socialNumberResponse.socialnumber)) {
                        findViewById(R.id.empty_view).setVisibility(View.VISIBLE);
                        findViewById(R.id.scroll_view).setVisibility(View.INVISIBLE);
                    } else {
                        findViewById(R.id.empty_view).setVisibility(View.INVISIBLE);
                        findViewById(R.id.scroll_view).setVisibility(View.VISIBLE);
                    }
                }
            } else if(request.requestCode == HTTP_CODE_SIGN_CODE) {
                if(response.targetData instanceof SocialSecuritySign) {
                    SocialSecuritySign sign = (SocialSecuritySign) response.targetData;

                    String config = Biap.getInstance().getMainUrl() + "?" + sign.sign;
                    Log.e("TAG", "config = " + config);

                    EsscSDK.getInstance().startSdk(this, config, new ESSCCallBack() {
                        @Override
                        public void onESSCResult(String s) {
                            Log.e("TAG", "s = " + s);
                        }
                    });
                }
            }
        }
    }
}
